import { createRouter, createWebHistory } from 'vue-router'
import Home from '@/views/Home.vue'
import UserApproval from '@/views/UserApproval.vue'
import FarmProducts from '@/views/FarmProducts.vue'
import ProductDetail from '@/views/ProductDetail.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: Home,
      meta: {
        title: '鼠鼠农业网 - 首页'
      }
    },
    {
      path: '/user-approval',
      name: 'userApproval',
      component: UserApproval,
      meta: {
        title: '用户审批',
        requiresAuth: true
      }
    },
    {
      path: '/profile',
      name: 'Profile',
      component: () => import('@/views/Profile.vue'),
      meta: { 
        title: '个人中心',
        requiresAuth: true 
      }
    },
    {
      path: '/wallet',
      name: 'Wallet',
      component: () => import('@/views/Wallet.vue'),
      meta: { 
        title: '我的钱包',
        requiresAuth: true 
      }
    },
    {
      path: '/my-farms',
      name: 'MyFarms',
      component: () => import('@/views/MyFarms.vue'),
      meta: { 
        title: '我的植物园',
        requiresAuth: true 
      }
    },
    {
      path: '/my-products',
      name: 'MyProducts',
      component: () => import('@/views/MyProducts.vue'),
      meta: { 
        title: '我的农产品',
        requiresAuth: true 
      }
    },
    {
      path: '/create-farm',
      name: 'CreateFarm',
      component: () => import('@/views/CreateFarm.vue'),
      meta: { 
        title: '创建植物园',
        requiresAuth: true 
      }
    },
    {
      path: '/farm-approval',
      name: 'FarmApproval',
      component: () => import('@/views/FarmApproval.vue'),
      meta: {
        title: '农场项目审批'
      }
    },
    {
      path: '/farm/detail/:farmId',
      name: 'FarmDetail',
      component: () => import('@/views/FarmDetail.vue'),
      props: route => ({ 
        farmId: route.params.farmId,
        userId: route.query.userId 
      }),
      meta: {
        title: '植物园详情'
      }
    },
    {
      path: '/farm/products/:farmId',
      name: 'FarmProducts',
      component: FarmProducts,
      meta: {
        title: '植物园商品'
      }
    },
    {
      path: '/product/:farmId/:productId',
      name: 'ProductDetail',
      component: ProductDetail,
      meta: {
        title: '商品详情'
      }
    },
    {
      path: '/category/:categoryId',
      name: 'CategoryProducts',
      component: () => import('@/views/CategoryProducts.vue')
    },
    {
      path: '/search',
      name: 'SearchResults',
      component: () => import('@/views/SearchResults.vue'),
      props: route => ({ keyword: route.query.keyword })
    },
    {
      path: '/orders',
      name: 'orders',
      component: () => import('@/views/Orders.vue')
    },
    {
      path: '/cart',
      name: 'cart',
      component: () => import('@/views/Cart.vue'),
      meta: {
        requiresAuth: true
      }
    },
    {
      path: '/knowledge',
      name: 'Knowledge',
      component: () => import('@/views/Knowledge.vue'),
      meta: {
        title: '农业知识大讲堂'
      }
    },
    {
      path: '/knowledge/article/:id',
      name: 'articleDetail',
      component: () => import('@/views/ArticleDetail.vue'),
      props: true
    },
    {
      path: '/expert-qa',
      name: 'ExpertQA',
      component: () => import('@/views/ExpertQA.vue'),
      meta: {
        title: '专家问答',
        requiresAuth: true
      }
    },
    {
      path: '/expert-qa/chat/:expertId',
      name: 'expertChat',
      component: () => import('@/views/ExpertChat.vue'),
      props: true,
      meta: { requiresAuth: true }
    },
    {
      path: '/favorites',
      name: 'Favorites',
      component: () => import('@/views/Favorites.vue'),
      meta: {
        title: '我的收藏',
        requiresAuth: true
      }
    },
    {
      path: '/user-management',
      name: 'UserManagement',
      component: () => import('@/views/UserManagement.vue'),
      meta: {
        title: '用户管理'
      }
    },
    {
      path: '/product-management',
      name: 'ProductManagement',
      component: () => import('@/views/ProductManagement.vue'),
      meta: {
        title: '农产品管理'
      }
    },
    {
      path: '/article-management',
      name: 'ArticleManagement',
      component: () => import('@/views/ArticleManagement.vue'),
      meta: {
        title: '文章管理'
      }
    },
    {
      path: '/order-management',
      name: 'OrderManagement',
      component: () => import('@/views/OrderManagement.vue'),
      meta: {
        title: '订单管理',
        requiresAuth: true
      }
    }
  ]
})

// 全局导航守卫，用于更新页面标题
router.beforeEach((to, from, next) => {
  // 更新页面标题
  document.title = to.meta.title || '鼠鼠农业网'
  next()
})

export default router 